{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1  [1 2 0 1 -1 0 -1]\n",
      "epoch 0 sample 2  [0 2 -1 1 0 -1 -1]\n",
      "epoch 0 sample 3  [0 1 -2 0 1 1 1]\n",
      "epoch 1 sample 0  [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 1  [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 2  [1 2 -1 1 0 -1 -1]\n",
      "epoch 1 sample 3  [1 1 -2 0 1 1 1]\n",
      "epoch 2 sample 0  [2 2 -1 0 0 0 0]\n",
      "epoch 2 sample 1  [2 2 -1 1 -1 0 -1]\n",
      "epoch 2 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 2 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 3 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 4 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 5 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 6 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 7 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 8 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 9 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 3  [1 2 -2 1 0 0 0]\n",
      "logical or\n",
      "epoch 0 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 1 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 2 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 3 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 4 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 5 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 6 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 7 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 8 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 9 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 3  [1 2 0 1 0 0 0]\n",
      "logical xor\n",
      "epoch 0 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3  [1 2 0 1 -1 -1 -1]\n",
      "epoch 1 sample 0  [0 1 -1 0 0 0 0]\n",
      "epoch 1 sample 1  [0 1 -1 0 1 0 1]\n",
      "epoch 1 sample 2  [1 1 0 1 0 0 0]\n",
      "epoch 1 sample 3  [1 1 0 1 -1 -1 -1]\n",
      "epoch 2 sample 0  [0 0 -1 0 0 0 0]\n",
      "epoch 2 sample 1  [0 0 -1 0 1 0 1]\n",
      "epoch 2 sample 2  [1 0 0 0 0 1 1]\n",
      "epoch 2 sample 3  [1 1 1 1 -1 -1 -1]\n",
      "epoch 3 sample 0  [0 0 0 0 0 0 0]\n",
      "epoch 3 sample 1  [0 0 0 0 1 0 1]\n",
      "epoch 3 sample 2  [1 0 1 1 0 0 0]\n",
      "epoch 3 sample 3  [1 0 1 1 -1 -1 -1]\n",
      "epoch 4 sample 0  [0 -1 0 0 0 0 0]\n",
      "epoch 4 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 4 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 4 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 5 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 5 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 5 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 5 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 6 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 6 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 6 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 6 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 7 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 7 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 7 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 7 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 8 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 8 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 8 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 8 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 9 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 9 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 9 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 9 sample 3  [1 0 2 1 -1 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "#第六周作业\n",
    "#!/usr/bin/env python3\n",
    "import numpy as np\n",
    "\n",
    "samples_and = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 0],\n",
    "    [0, 1, 0],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "\n",
    "samples_or = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "#异或\n",
    "samples_xor = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 0],\n",
    "]\n",
    "\n",
    "\n",
    "#感知机的学习规则，所谓学习规则就是修改神经网络的权值和偏置值得方法和过程。（训练过程）\n",
    "#感知机的学习规则属于有监督学习\n",
    "def perceptron(samples):\n",
    "    #权重系数\n",
    "    w = np.array([1, 2])\n",
    "    #偏置\n",
    "    b = 0\n",
    "    #学习率\n",
    "    a = 1\n",
    "\n",
    "    for i in range(10):\n",
    "        for j in range(4):\n",
    "            #取每个行向量的前两个\n",
    "            x = np.array(samples[j][:2])\n",
    "            #（三目运算）激励函数：阶跃函数\n",
    "            y = 1 if np.dot(w, x) + b > 0 else 0\n",
    "            #取每个行向量的第三列\n",
    "            d = np.array(samples[j][2])\n",
    "            #损失函数对b的梯度为：-（t-y）\n",
    "            #d-y 是梯度下降的方向 a是学习率。\n",
    "            delta_b = a*(d-y)\n",
    "            #损失函数对w的梯度为：-（t-y）*x\n",
    "            delta_w = a*(d-y)*x\n",
    "            #delta_b，delta_w均为0的时候，表示收敛\n",
    "            print('epoch {} sample {}  [{} {} {} {} {} {} {}]'.format(\n",
    "                i, j, w[0], w[1], b, y, delta_w[0], delta_w[1], delta_b\n",
    "            ))\n",
    "            #更新权重系数与偏置项\n",
    "            w = w + delta_w\n",
    "            b = b + delta_b\n",
    "\n",
    "#每个python文件都包含一个内置变量 __name__\n",
    "#__main__ 是本文件的文件名称。\n",
    "if __name__ == '__main__':\n",
    "    print('logical and')\n",
    "    perceptron(samples_and)\n",
    "    print('logical or')\n",
    "    perceptron(samples_or)\n",
    "    print('logical xor')\n",
    "    perceptron(samples_xor)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "恋习Python？？\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
